
var vectorlayer =null;
var dragControl = null;

var marklayer = null;

var yydResArray = [];
var yydMarkArray = [];
var yydPopupArray = [];
//已有点/////////////////////////////////////////////////////////////////
//上传之前检查上传的文件是不是MP3
function checkYydFileType(value){
	var type = value.substring(value.length-3,value.length);
	if(type!="mp3"&&type!="MP3"){
		alert("请上传mp3格式的文件");
		$("#yydDMT").attr("value","");
	}
}

function queryyydDDSB(){
	debugger;
	var shen = $("#yydShen").val();
	var shi = $("#yydShi").val();
	var xian = $("#yydXian").val();
	var areaCode = handlerAreaFunc(shen,shi,xian);
	var zhlx = $("#yydZHLX").val();
	var zhCode = "";
	if(zhlx==""){
		zhCode = handlerZhCodeFunc(areaCode);
	}else{
		zhCode = handlerZhCodeFunc(areaCode,zhlx);
	}
	var mc = $("#yydMC").val();
	var data = {
			"zhCode":zhCode,
			"mc":mc
		};
	var url = "queryJBQKForXxbs.shtml";
	yydResArray = [];
	$("#tblyyd").datagrid({
		url : url,
		queryParams : data,
		onLoadError : function(res) {
			alert("查询失败!");
		},
		onLoadSuccess : function(res) {
			//打mark
			//modefy by cuiyi 20130107 统一灾害点定位。
			//addMarkForYYD();
			top.addMarkAndPopup(res.rowsTemp)
		}
	});
}

//加载列表成功之后，调用此函数，给打mark
function addMarkForYYD(){
	
	if(marklayer!=null){
		top.map.removeLayer(marklayer);
	}
	marklayer = new OpenLayers.Layer.Markers("marklayer");
	top.map.addLayer(marklayer);
	for(var i =0;i<yydResArray.length;i++){
		var mark = createYydMark(yydResArray[i]);
		mark.index= i;
		yydMarkArray.push(mark);
		marklayer.addMarker(mark);
		var popup = createYydPopup(yydResArray[i]);
		yydPopupArray.push(popup);
		top.map.addPopup(popup);
		popup.hide();
		mark.events.register("mousedown",mark,yydMarkClick);
	}
}

//mark的点击事件
function yydMarkClick(evt){
	var mark = evt.object;
	yydPopupArray[mark.index].toggle();
}

//制造mark
function createYydMark(data){
	var size = new OpenLayers.Size(30, 30);
	var icon = new OpenLayers.Icon('image/marker.png', size);
	var merkatorPt = top.lonLat2Mercator(data.jbqk0120,data.jbqk0130);
	var mark = new OpenLayers.Marker(new OpenLayers.LonLat(merkatorPt.x,merkatorPt.y),icon);
	return mark;
}

//制造popup
function createYydPopup(obj){
	var context = '<div  style="color:#056F89;width:260px; height:140px;font-size:12px">' +
    '<div  style="height:15px;width:260px;position:absolute; top:0px;margin-top:0;">' +
    '<div style="width:200px;position:absolute;" >' +
        '<label style=" font-size:13px; font-weight:bold;">' + obj.id.jbqk0110 + '</label>' +
    '</div>' +
    '<div style="position:absolute;right:0;width:60px;">' +
        '<a href="" target="_blank">详细信息</a>' +
    '</div>' +
    '</div>' +
    '<div style="height:1px; width:260px; background:#CECECE; margin-top:15px;right:0;"></div>' +
    '<div style="height:88px;margin-top:15px;">' +
    '<div style="position:absolute;top:35px;width:116px; height:88px; padding:5px 0;">' +
    '<label>经度：</label>' +
	'<label>' + obj.id.jbqk0120 + '</label>' +
	'<br/>' +
	'<label>纬度：</label>' +
	'<label>' + obj.id.jbqk0130 + '</label>' +
	'<br/>' +
	'<label>简介链接：</label>' +
	'<label>' + obj.id.jbqk0170 + '</label>' +
    '<br/>' +
    '<label>编号：</label>' +
	'<label>' + obj.id.jbqk0100 + '</label>' +
    '<br/>' +
	'</div>' +
        '<div style="position:absolute;top:35px;margin:0 0 0 0px;height:88px;right:15px;width:110px;border:1px solid #333333; background-repeat:no-repeat;background-image:url(images/imageForMark/' + obj.markImage + ')"></div>' +
    '</div>' +
    '<div style="position:absolute; width:260px;">' +
    '<div style="position:absolute; width:40px;height:15px;margin-top:3px;">' +
    '<label>地点：</label>' +
    '</div>' +
    '<div style="position:absolute;left:35px; width:220px; height:15px;margin-top:3px;">' +
	'<label>' + obj.id.jbqk0140 + ''+obj.id.jbqk0150+''+obj.id.jbqk0160+'</label>' +
    '</div>' +
    '</div>' +
'</div>';
var popup = new OpenLayers.Popup.NestFramedCloud(null, new OpenLayers.LonLat(obj.jbqk0120,obj.jbqk0130), new OpenLayers.Size(260,120), context, null, false, null);
return popup;
}

function inittblyyd(){
	$("#tblyyd").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[ /*{
					field : 'id_jbqk0100',
					title : '统一编码',
					width : 120,
					align : 'center',
					editor : 'text'
				},*/
				{
					field : 'jbqk0110',
					title : '地质灾害名称',
					width : 100,
					align : 'center',
					editor : 'text'
				},
				{
					field : 'id_jbqk0102',
					title : '灾害点标识',
					width : 100,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex){
					
						var type = top.getOperTypeZhdByCode(rec.id.jbqk0102);
						var resutsHTML = "<div style='align:center;'>"
								+type
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'jbqk0140',
					title : '乡镇',
					width : 60,
					align : 'center',
					editor : 'text'
				},{
					field : 'jbqk0150',
					title : '村',
					width : 60,
					align : 'center',
					editor : 'text'
				},{
					field : 'jbqk0160',
					title : '组',
					width : 60,
					align : 'center',
					editor : 'text'
				},{
					field : 'oper',
					title : '填写上报内容',
					width : 120,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						yydResArray.push(rec);
						var num = yydResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='showJBQKDetails(\""
								+ num
								+ "\",false)'>填写</a>"
								+"</div>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
	});
}

function showJBQKDetails(index){
	var curObj = yydResArray[index];
	//各种赋值
	$("#yydDMT").attr("value","");
	$("#yydSBNR").attr("value","");
	$("#yydNum").attr("value",curObj.id.jbqk0100).attr("readOnly",'readOnly');
	$("#yydLon").attr("value",curObj.jbqk0120).attr("readOnly","readOnly");
	$("#yydLat").attr("value",curObj.jbqk0130).attr("readOnly","readOnly");
	$("#yydBiaoshi").attr("value",curObj.id.jbqk0102);
	/////cuiyi 20120823
	var jbqk0140 = curObj.jbqk0140=="null"|| curObj.jbqk0140==null?"":curObj.jbqk0140;
	var jbqk0150 = curObj.jbqk0150=="null"||curObj.jbqk0150==null?"":curObj.jbqk0150;
	var jbqk0160 = curObj.jbqk0160=="null"||curObj.jbqk0160==null?"":curObj.jbqk0160;
	$("#yydDLWZ").attr("value",jbqk0140+jbqk0150+jbqk0160).attr("readOnly","readOnly");
	//对话框打开
	$("#yydWrite").dialog("open");
}

//重置上报信息
function resetyydEvent(){
	$("#yydTime").val("");
	$("#yydSBNR").val("");
	$("#yydDMT").val("");
}

//点击已有点提交按钮
function submityydEvent(){
	if($("#yydTime").val()==""){
		alert("时间必须填写");
		return;
	}
	$("#yydForm").submit(function () {
        // 提交表单
        $(this).ajaxSubmit();
        // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false
        return false;
    });
    $('#yydForm').ajaxSubmit(function (res) {
    	if(res.length>4){
   		 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
     		var hiddenDiv = $("<div>");
     		hiddenDiv.html(res);
     		res = hiddenDiv.text();
   		}
    	if(res=="true"){
    		alert("上报成功！");
    		alert("请进行研判信息上报工作!");
    	}else{
    		alert("上传失败！");
    	}
    	$("#yydWrite").dialog("close");
    });
}

//新增点/////////////////////////////////////////////////////////////////
//清除所有的信息
function resetxzdEvent(){
	$("#xzdShi").val("");
	$("#xzdXian").val("");
	$("#xzdZHLX").val("");
	$("#xzdTime").val("");
	$("#xzdSBNR").val("");
	$("#xzdDMT").val("");
	$("#xzdDLWZ").val("");
}

//lon和lat的初始化函数
function initLonLatEvtForXZD(){
//	//先在地图上面添加一个点要素到一个矢量图层之中去
//	vectorlayer = new OpenLayers.Layer.Vector("vectorlayer");
//	//初始化坐标点位置，现在随便一个
//	var point  = new OpenLayers.Geometry.Point(111.4750335,31.2852563);
//	var pointFeature = new OpenLayers.Feature.Vector(point);
//	top.map.addLayer(vectorlayer);
//	vectorlayer.addFeatures(pointFeature);
//	//激活可以拖动要素
//	dragControl = new OpenLayers.Control.DragFeature(vectorlayer,{onDrag:onDragPointFeature});
//	top.map.addControl(dragControl);
//	dragControl.activate();
	//赋值到文本框
}

//拖动时候的回调
function onDragPointFeature(feature,pixel){
	var lon = feature.geometry.x ;
	var lat = feature.geometry.y;
	$("#xzdLon").val(lon);
	$("#xzdLat").val(lat);
}

//关闭对话框时，将图层和点都要清除
function removeLayerAndFeatureForLogic(){
	if(vectorlayer!=null){
		top.map.removeLayer(vectorlayer);
		vectorlayer = null;
	}
	if(dragControl!=null){
		top.map.removeControl(dragControl);
		vectorlayer = null;
	}
	if(marklayer!=null){
		top.map.removeLayer(marklayer);
		marklayer = null;
	}
}

//添加新增点的信息
function addxzdDDSB(){
	var sheng = $("#xzdShen").val();
	var  shi = $("#xzdShi").val();
	var xian =$("#xzdXian").val();
	var time =$("#xzdTime").val();
	var zhlx = $("#xzdZHLX").val();
	var lon = $("#xzdLon").val();
	var lat = $("#xzdLat").val();
	//封装数据
	if(lon==""){
		alert("经度必须填写");
	}
	if(lat==""){
		alert("纬度必须填写");
	}
	if(time==""){
		alert("时间必须填写");
		return;
	}
	if(shi==""){
		alert("市必须填写");
		return;
	}
	if(xian==""){
		alert("县必须填写");
		return;
	}
	if(zhlx==""){
		alert("灾害类型必须填写");
		return;
	}
	$("#xzdForm").submit(function () {
        // 提交表单
        $(this).ajaxSubmit();
        // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false
        return false;
    });
    $('#xzdForm').ajaxSubmit(function (res) {
    	if(res.length>4){
   		 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
     		var hiddenDiv = $("<div>");
     		hiddenDiv.html(res);
     		res = hiddenDiv.text();
   		}
    	if(res=="true"){
    		alert("上报成功！");
    		alert("请进行研判信息上报工作!");
    		alert("请进行预案匹配工作!");
    	}else{
    		alert("上传失败！");
    	}
    });
}

//上传多媒体文件时，对于多媒体的判断
function checkXzdFileType(value){
	var type = value.substring(value.length-3,value.length);
	if(type!="mp3"&&type!="MP3"){
		alert("请上传mp3格式的文件");
		$("#xzdDMT").attr("value","");
	}
}